﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using TextBox = System.Web.UI.WebControls.TextBox;

namespace asp.netWebFroms
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataFill();
            }
        }

        private void DataFill()
        {
            var sql = string.Format("select * from Articles");
            var dt = DbHelper.Table(sql);
            gv1.DataSource = dt;
            gv1.DataBind();
        }

        public DataTable Data() 
        {
            var selelt =string.Format("select * from Articles") ;
            var dt = DbHelper.Table(selelt);
            return dt;
        }
        //查询
        protected void select_Click(object sender, EventArgs e)
        {
            var sql = string.Format(" select * from Articles where Title like '%{0}%' or Content like '%{1}%' or Author like '&{2}&' or Remarks like '%{3}%' ", tb1.Text,tb1.Text,tb1.Text,tb1.Text);
            var dt = DbHelper.Table(sql);
            gv1.DataSource = dt;
            gv1.DataBind();
        }
        //添加 
        protected void Add_Click(object sender, EventArgs e)
        {
            var dt = Data().Copy();
            dt.Rows.Add(dt.NewRow());
            gv1.EditIndex = dt.Rows.Count - 1;
            gv1.DataSource = dt;
            gv1.DataBind();
        }

        //取消编辑
        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv1.EditIndex = -1;
            DataFill();
        }

        //进入编辑
        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            DataFill();
        }
        private string GetValues(int rowIndex, int colIndex)
        {
            var control = gv1.Rows[rowIndex].Cells[colIndex];
            var values = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return values;
        }
        //更新时
        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var IdString = GetValues(e.RowIndex,0);
            var Title= GetValues(e.RowIndex, 1);
            var Content= GetValues(e.RowIndex, 2);
            var Author= GetValues(e.RowIndex, 3);
            var Remarks = GetValues(e.RowIndex, 4);

            var IsOk = int.TryParse(IdString,out int Id);
            
            if (IsOk)
            {
                var update = string.Format("update Articles set Title='{0}',Content='{1}' ,Author='{2}' ,Remarks='{3}' where id={4}",Title,Content,Author,Remarks,Id);
                var dt = DbHelper.AddOrUpdateDelete(update);
                if (dt>0)
                {
                   MessageBox.Show("更新成功","提示");
                    gv1.EditIndex = -1;
                    DataFill();
                }
                else
                {
                    MessageBox.Show("更新失败", "提示");
                }
            }
           
            else
            {
                var insert = string.Format("insert into Articles(Title, Content, Author,Remarks) values('{0}', '{1}', '{2}','{3}')", Title, Content, Author, Remarks);
                var dt = DbHelper.AddOrUpdateDelete(insert);
                if (dt > 0)
                {
                    MessageBox.Show("添加成功", "提示");
                    gv1.EditIndex = -1;
                    DataFill();
                }
                else
                {
                    MessageBox.Show("添加失败", "提示");
                }
            }
            

        }

      
        //删除时
        protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GetValues(e.RowIndex, 0);

            var res = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (res.Equals(DialogResult.Yes))
            {
                var delete = string.Format("delete from Articles where id={0}",Id);
                var dt = DbHelper.AddOrUpdateDelete(delete);
                if (dt > 0)
                {
                    MessageBox.Show("删除成功", "提示");
                    DataFill();
                }
                else
                {
                    MessageBox.Show("删除失败", "提示");
                }
            }
            else
            {
                MessageBox.Show("你已取消删除", "提示");
            }
        }

        
    }
}